﻿/**********************************天使物流*******************************************
* 功能描述:系统设置——部门管理
* 作者:孟帅龙
* 时间:2022/1/10
* 
**********************************天使物流*******************************************/
using Snowflake.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.Common;
using TianShiProject.Models.System.Departments;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.System.Departments
{
    public class DepartmentRepository : DapperDbContextRepository, IDepartmentRepository
    {
        public DepartmentRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }

        #region 部门管理——查询分页显示
        /// <summary>
        /// 部门管理——查询分页显示
        /// </summary>
        /// <param name="DeptName"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<DepartmentDto>> GetDepartmentPageShow(string DeptName, int pageindex = 1, int pagesize = 3)
        {
            PageResponse<DepartmentDto> response = new PageResponse<DepartmentDto>();
            var sql = "select Id,DeptNumber,DeptName,SuperiorDept,CreateTime,IsDelete from Department where IsDelete=0";
            if (!string.IsNullOrWhiteSpace(DeptName))
            {
                sql += $" and DeptName like '%" + DeptName + "%'";
            }
            var list = await this.QueryAsync<DepartmentDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $" order by Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<DepartmentDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        #endregion

        #region 部门管理——逻辑删除
        /// <summary>
        /// 部门管理——逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<PageResponse<DepartmentDto>> DeleteDepartment(int id)
        {
            PageResponse<DepartmentDto> response = new PageResponse<DepartmentDto>();
            var sql = $"update Department set IsDelete=1 where Id={id}";
            response.Code = await ExecuteAsync(sql, null);
            return response;
        }
        #endregion

        #region 部门管理——批量删除
        /// <summary>
        /// 部门管理——批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<PageResponse<DepartmentDto>> DeleteBatchDepartment(string ids)
        {
            PageResponse<DepartmentDto> response = new PageResponse<DepartmentDto>();
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Department set IsDelete=1 where Id={item}";
                response.Code = await ExecuteAsync(sql, null);
            }
            return response;
        }
        #endregion

        #region 部门管理——编辑
        /// <summary>
        /// 部门管理——编辑
        /// </summary>
        /// <param name="departmentDto"></param>
        /// <returns></returns>
        public async Task<int> PostUpdateDepartment(DepartmentDto departmentDto)
        {
            PageResponse<DepartmentDto> response = new PageResponse<DepartmentDto>();
            var sql = $"update Department set DeptNumber='{departmentDto.DeptNumber}',DeptName='{departmentDto.DeptName}',SuperiorDept='{departmentDto.SuperiorDept}',CreateTime=getdate(),IsDelete=0 where Id='{departmentDto.Id}'";
            return await ExecuteAsync(sql, departmentDto);
        }
        #endregion

        #region 部门管理——添加
        /// <summary>
        /// 部门管理——添加
        /// </summary>
        /// <param name="rolesDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddDepartment(DepartmentDto departmentDto)
        {
            var worker = new IdWorker(1, 1);
            var id = worker.NextId().ToString();
            var sql = $"insert into Department values('{id}','{departmentDto.DeptName}','{departmentDto.SuperiorDept}',getdate(),0)";
            return await ExecuteAsync(sql, departmentDto);
        }
        #endregion

        #region 部门下拉框
        /// <summary>
        /// 部门下拉框
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<DepartmentDto>> GetDept()
        {
            PageResponse<DepartmentDto> response = new PageResponse<DepartmentDto>();
            var sql = $"select * from Department where IsDelete=0";
            response.PageData = await this.QueryAsync<DepartmentDto>(sql, null);
            return response;
        } 
        #endregion
    }
}
